<cnsl-create-layout
  title="{{ id ? ('IDP.DETAIL.TITLE' | translate) : ('IDP.CREATE.TITLE' | translate) }}"
  (closed)="close()"
>
  <div class="identity-provider-create-content">
    <div class="identity-provider-title-row">
      <i class="idp-icon las la-building"></i>
      <h1>{{ 'IDP.CREATE.LDAP.TITLE' | translate }}</h1>
      <ng-container *ngIf="exists$ | async">
        <div
          *ngIf="{ isNotActive: (activateLink$ | async) } as idp"
          class="cnsl-state-dot"
          [matTooltip]="(idp.isNotActive ? 'IDP.STATES.2' : 'IDP.STATES.1') | translate"
          [ngClass]="{ active: !idp.isNotActive, inactive: !!idp.isNotActive }"
        ></div>
      </ng-container>
      <mat-spinner diameter="25" *ngIf="loading" color="primary"></mat-spinner>
    </div>

    <cnsl-provider-next
      [configureProvider]="(justCreated$ | async) === ''"
      [configureTitle]="'DESCRIPTIONS.SETTINGS.IDPS.LDAP.TITLE' | translate: { provider: 'Google' }"
      [configureDescription]="'DESCRIPTIONS.SETTINGS.IDPS.LDAP.DESCRIPTION' | translate: { provider: 'Google' }"
      configureLink="https://zitadel.com/docs/guides/integrate/identity-providers/ldap"
      [activateLink]="activateLink$ | async"
      [expanded]="!!(expandWhatNow$ | async)"
      (activate)="activate()"
    ></cnsl-provider-next>

    <p class="identity-provider-desc cnsl-secondary-text">
      {{ !provider ? ('IDP.CREATE.LDAP.DESCRIPTION' | translate) : ('IDP.DETAIL.DESCRIPTION' | translate) }}
    </p>

    <form [formGroup]="form" (ngSubmit)="submitForm()">
      <div class="identity-provider-content">
        <cnsl-form-field class="formfield">
          <cnsl-label>{{ 'IDP.NAME' | translate }}</cnsl-label>
          <input cnslInput formControlName="name" required />
        </cnsl-form-field>

        <h2 class="subheader">{{ 'IDP.LDAPCONNECTION' | translate }}</h2>

        <cnsl-string-list
          class="string-list-component-wrapper"
          title="{{ 'IDP.SERVERS' | translate }}"
          formControlName="serversList"
          [required]="true"
        ></cnsl-string-list>

        <cnsl-form-field class="formfield">
          <cnsl-label>{{ 'IDP.BASEDN' | translate }}</cnsl-label>
          <input cnslInput formControlName="baseDn" required />
        </cnsl-form-field>

        <div [ngClass]="{ 'identity-provider-2-col': !provider }">
          <cnsl-form-field class="formfield">
            <cnsl-label>{{ 'IDP.BINDDN' | translate }}</cnsl-label>
            <input cnslInput formControlName="bindDn" required />
          </cnsl-form-field>

          <mat-checkbox
            class="update-secret-checkbox"
            *ngIf="provider"
            [(ngModel)]="updateBindPassword"
            [ngModelOptions]="{ standalone: true }"
            >{{ 'IDP.UPDATEBINDPASSWORD' | translate }}</mat-checkbox
          >
          <cnsl-form-field class="formfield pwd" [ngClass]="{ show: !provider || (provider && updateBindPassword) }">
            <cnsl-label>{{ 'IDP.BINDPASSWORD' | translate }}</cnsl-label>
            <input
              cnslInput
              formControlName="bindPassword"
              type="password"
              autocomplete="new-password"
              [required]="!provider"
            />
          </cnsl-form-field>
        </div>

        <h2 class="subheader">{{ 'IDP.LDAPUSERBINDING' | translate }}</h2>

        <cnsl-form-field class="formfield">
          <cnsl-label>{{ 'IDP.USERBASE' | translate }}</cnsl-label>
          <input cnslInput formControlName="userBase" required />
        </cnsl-form-field>

        <cnsl-string-list
          class="string-list-component-wrapper"
          title="{{ 'IDP.USERFILTERS' | translate }}"
          formControlName="userFiltersList"
          [required]="true"
        ></cnsl-string-list>

        <cnsl-string-list
          class="string-list-component-wrapper"
          title="{{ 'IDP.USEROBJECTCLASSES' | translate }}"
          formControlName="userObjectClassesList"
          [required]="true"
        ></cnsl-string-list>

        <div class="identity-provider-optional-h-wrapper">
          <h2>{{ 'IDP.LDAPATTRIBUTES' | translate }}</h2>
        </div>
        <cnsl-ldap-attributes
          [initialAttributes]="provider?.config?.ldap?.attributes"
          (attributesChanged)="attributes = $event"
        ></cnsl-ldap-attributes>

        <div class="identity-provider-optional-h-wrapper">
          <h2>{{ 'IDP.OPTIONAL' | translate }}</h2>
          <button (click)="showOptional = !showOptional" type="button" mat-icon-button>
            <mat-icon *ngIf="showOptional">keyboard_arrow_up</mat-icon
            ><mat-icon *ngIf="!showOptional">keyboard_arrow_down</mat-icon>
          </button>
        </div>
        <div *ngIf="showOptional">
          <mat-checkbox formControlName="startTls">{{ 'IDP.STARTTLS' | translate }}</mat-checkbox>

          <cnsl-form-field class="formfield">
            <cnsl-label>{{ 'IDP.TIMEOUT' | translate }}</cnsl-label>
            <input cnslInput formControlName="timeout" type="number" />
          </cnsl-form-field>

          <cnsl-provider-options
            [initialOptions]="provider?.config?.options"
            (optionsChanged)="options = $event"
          ></cnsl-provider-options>
        </div>
      </div>

      <div class="identity-provider-create-actions">
        <button
          color="primary"
          mat-raised-button
          class="continue-button"
          [disabled]="!form.valid || !attributes.toObject().idAttribute || form.disabled"
          type="submit"
        >
          <span *ngIf="{ exists: (exists$ | async) } as idp">{{
            (!!idp.exists ? 'ACTIONS.SAVE' : 'ACTIONS.CREATE') | translate
          }}</span>
        </button>
      </div>
    </form>
  </div>
</cnsl-create-layout>
